<template>
  <page-header-wrapper :title="false">
    <a-card title="成本统计">
      <div class="table-page-search-wrapper">
        <a-form-model :model="queryParam" layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="合同编号">
                <a-input v-model="queryParam['filter[contract_no]']" placeholder="输入合同编号" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="业务编号">
                <a-input v-model="queryParam['filter[business_no]']" placeholder="输入业务编号" />
              </a-form-model-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-item label="打款时间">
                  <a-range-picker @change="changePaymentTime" />
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
              <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 5px" @click="reset">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <div class="table-operator">
        <s-table
          bordered
          ref="table"
          :row-key="(record) => record.payment_id"
          :columns="statsColumns"
          :data="statsData"
        >
          <template slot="title">
            <div style="text-align: right">
              <a-button size="small" icon="export" type="primary" @click="onExport('2')">导出已付成本业务</a-button>
            </div>
          </template>
          <template slot="ix" slot-scope="text,record,index">
            {{index + 1}}
          </template>
          <template slot="contract_no" slot-scope="text,record">
            <a-button type="link" @click="$router.push({ name: 'ContractDetail', params: { id: record.contract_id } })">{{text}}</a-button>
          </template>
          <template slot="amount" slot-scope="text">
            <a-tag color="red">{{text}}</a-tag>
          </template>
          <template slot="agency_fee" slot-scope="text">
            <a-tag color="red">{{text}}</a-tag>
            <!--            <a-divider type="vertical" />-->
            <!--            <a-tag color="red">{{text}}</a-tag>-->
          </template>
        </s-table>
      </div>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { STable } from '@/components'
import statsColumns from '@/columns/stats'
import apis from '@/api'
import storage from 'store'

export default {
  components: {
    STable
  },
  name: 'CostStats',
  data () {
    return {
      statsColumns: statsColumns.cost,
      statsData: () => {
        return new Promise(resolve => {
          resolve({
            page: 1,
            page_size: 10,
            total_count: 0,
            data: []
          })
        })
      },
      queryParam: {},
      advanced: true,
      exportUrl: apis.ExportData,
      accessToken: storage.get('Access-Token'),
      company: storage.get('company')
    }
  },
  methods: {
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    reset () {
      this.queryParam = {}
      this.$refs.table.refresh(true)
    },
    changePaymentTime (date, dateString) {
      this.queryParam['filter[payment_time_start]'] = dateString[0]
      this.queryParam['filter[payment_time_end]'] = dateString[1]
    },
    onExport (type) {
      delete this.queryParam['check_time']
      let params = this.exportUrl + '?access_token=' + this.accessToken + '&type=' + type + '&company=' + this.company
      if (type === '1') {
        for (const k in this.queryParam) {
          params = params + '&' + k + '=' + this.queryParam[k]
        }
        window.open(process.env.VUE_APP_API_BASE_URL + params)
      }
      if (type === '2') {
        params = params + '&filter[payment_time_start]=' + this.queryParam['filter[payment_time_start]'] + '&filter[payment_time_end]=' + this.queryParam['filter[payment_time_end]']
        window.open(process.env.VUE_APP_API_BASE_URL + params)
      }
    }
  }
}
</script>

<style scoped>

</style>
